package com.lzx.lzxlibrary.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.lzx.library.dowload.DownloadBean;
import com.lzx.library.dowload.DownloadListener;
import com.lzx.library.dowload.DownloadManager;
import com.lzx.library.rx.RxHttpManager;
import com.lzx.lzxlibrary.R;
import com.lzx.lzxlibrary.bean.IndexBean;
import com.lzx.lzxlibrary.bean.LoginBean;
import com.lzx.lzxlibrary.bean.ReportBean;
import com.rxjava.rxlife.RxLife;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import rxhttp.wrapper.param.RxHttp;

/**
 * <pre>
 *     author : 张辉
 *     e-mail : 397243131@qq.com
 *     time   : 2021/07/28
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class DemoActivity extends AppCompatActivity implements View.OnClickListener, DownloadListener {

    private TextView tv_content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }


    private void initView() {
        tv_content = findViewById(R.id.tv_content);
        findViewById(R.id.tv_btn_http).setOnClickListener(this::onClick);
        findViewById(R.id.post_list).setOnClickListener(this::onClick);
        findViewById(R.id.post_page).setOnClickListener(this::onClick);
        findViewById(R.id.tv_btn_download).setOnClickListener(this::onClick);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_btn_http:
                post();
                break;
            case R.id.post_list:
                postList();
                break;
            case R.id.post_page:
                postPage();
                break;
            case R.id.tv_btn_download:
                DownloadManager.getInstance().versionInfo(this, this);
                break;
        }
    }


    @Override
    public void onProgress(int progress, long currentSize, long totalSize) {
        LogUtils.i("dowload" + progress + "/100" + "     " + currentSize + "/" + totalSize);
    }

    @Override
    public void onSuccess(String url, String msg) {
        LogUtils.i("dowload" + url + "====" + msg);
        DownloadManager.getInstance().download(url, this);

    }

    @Override
    public void onDownloadSuccess(String path) {
        LogUtils.i("dowload" + path);

    }

    @Override
    public void onFaile(String msg) {

    }

    private String accessToken = "";

    private void post() {

        Map<String, String> params = new HashMap<>();
        params.put("password", "123456");
        params.put("username", "admin");
        RxHttp.postForm("/v1/login").
                addAll(params).
                asResponse(LoginBean.class).
                as(RxLife.asOnMain(this)).
                subscribe(new Consumer<LoginBean>() {
                    @Override
                    public void accept(LoginBean loginBean) throws Exception {
                        accessToken = loginBean.getAccess_token();
                        RxHttpManager.getInstance().setAccessToken(loginBean.getAccess_token());
                        tv_content.setText(GsonUtils.toJson(loginBean));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        tv_content.setText(throwable.getMessage());
                        LogUtils.e(throwable.getMessage());
                    }
                });


    }

    private void postList() {
        Map<String, Object> params = new HashMap<>();
        params.put("access_token", accessToken);
        RxHttp.get("/f/v1/modules/index").addAll(params).asResponseList(IndexBean.class).as(RxLife.asOnMain(this)).subscribe(new Consumer<List<IndexBean>>() {
            @Override
            public void accept(List<IndexBean> indexBeans) throws Exception {
                tv_content.setText(tv_content.getText().toString() + "\n\n\n" + GsonUtils.toJson(indexBeans));
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                tv_content.setText(tv_content.getText().toString() + "\n\n\n" + throwable.getMessage());
            }
        });
    }

    private void postPage() {
        Map<String, Object> params = new HashMap<>();
        params.put("createdBy", "admin");
        params.put("access_token", accessToken);
        RxHttp.get("/f/v1/report").addAll(params).asResponse(ReportBean.class).as(RxLife.asOnMain(this)).subscribe(new Consumer<ReportBean>() {
            @Override
            public void accept(ReportBean reportBean) throws Exception {
                tv_content.setText(tv_content.getText().toString() + "\n\n\n" + GsonUtils.toJson(reportBean));
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                tv_content.setText(tv_content.getText().toString() + "\n\n\n" + throwable.getMessage());
            }
        });
    }
}
